package com.myGis.finder;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import com.myGis.entities.Layer;
import com.myGis.entities.User;
import com.myGis.entities.ValueField;

public class FinderData<T> implements IFinderData {

	private Session session;
	private Class entity;

	public FinderData(Session session) {
		this.session = session;
	}

	public FinderData(Session session, Class entity) {
		this.session = session;
		this.entity = entity;
	}

	@Override
	public <T> List<T> findAll() {
		Criteria criteria = session.createCriteria(this.entity);
		return (List<T>) criteria.list();
	}
	
	public List<Object> findSpatialObjectKMLByLayer(Layer layer) {
		Query query = session.createSQLQuery("SELECT spatial_object_id , ST_AsKML( ST_Transform( geometry, 4326 ) ) as kml  from mygis_spatial_object where layer_id = :layerId").setParameter("layerId", layer.getLayerId());
		return (List<Object>) query.list();
	}

	@Override
	public List findUser(String n) {
		Criteria criteria = session.createCriteria(User.class);
		if(!"".equals(n)){
			criteria.add(Expression.eq("userName", n));
		}
		return criteria.list();
	}

	@Override
	public List findValueFieldById(Long id) {
		Criteria criteria = session.createCriteria(ValueField.class);
		List list = criteria.add(Restrictions.eq("valueFieldId", id)).list();
		return list;
	}
}
